//
//  YDMenuController.m
//  YDBASE
//
//  Created by rowena on 2019/4/22.
//  Copyright © 2019年 wuyezhiguhun. All rights reserved.
//

#import "YDMenuController.h"
#import "YDModuleRouterURL.h"

@interface YDMenuController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *menuTableView;
@property (nonatomic, copy) NSArray *routerURLArray;
@property (nonatomic, copy) NSArray *routerNameArray;

@end

@implementation YDMenuController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"MENU";
    
    [self.view addSubview:self.menuTableView];
    // Do any additional setup after loading the view.
}

- (void)displayAppPrivacySettings {
    
    if (@available(iOS 8,*)) {
        if (UIApplicationOpenSettingsURLString != NULL) {
            NSURL *appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            //NSURL *appSettings = [NSURL URLWithString:@"prefs:root=Bluetooth"];
            
            if (@available(iOS 10,*)) {
                [[UIApplication sharedApplication] openURL:appSettings options:@{} completionHandler:^(BOOL success) {
                }];
            } else {
                [[UIApplication sharedApplication] openURL:appSettings];
            }
        }
    }
}

#pragma mark ==== UITableViewDataSource ====
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.routerURLArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *tableViewCell = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [self.routerNameArray objectAtIndex:indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

#pragma mark ==== UITableViewDelegate ====
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *viewController = [[YDRouter shared] matchController:[self.routerURLArray objectAtIndex:indexPath.row]];
    viewController.title = [self.routerNameArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:viewController animated:YES];
    
    //[self displayAppPrivacySettings];
}


- (UITableView *)menuTableView {
    if (!_menuTableView) {
        _menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, YDScreenWidth, YDScreenHeight - 64) style:UITableViewStylePlain];
        _menuTableView.backgroundColor = [UIColor clearColor];
        _menuTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _menuTableView.delegate = self;
        _menuTableView.dataSource = self;
    }
    return _menuTableView;
}

- (NSArray *)routerURLArray {
    if (!_routerURLArray) {
        _routerURLArray = @[YDModuleRouterRuntimeURL, YDModuleRouterThreadURL, YDModuleRouterRunLoopURL, YDModuleRouterCategoryURL, YDModuleRouterEventsURL, YDModuleRouterOCFeatureURL, YDModuleRouterPropertyURL, YDModuleRouterBlockURL, YDModuleRouterDatabaseURL];
    }
    return _routerURLArray;
}

- (NSArray *)routerNameArray {
    if (!_routerNameArray) {
        _routerNameArray = @[YDModuleRouterRuntimeName, YDModuleRouterThreadName, YDModuleRouterRunLoopName, YDModuleRouterCategoryName, YDModuleRouterEventsName, YDModuleRouterOCFeatureName, YDModuleRouterPropertyName, YDModuleRouterBlockName, YDModuleRouterDatabaseName];
    }
    return _routerNameArray;
}


@end
